#ifndef YXMYSQL_H
#define YXMYSQL_H
#include "../../include/mysql/mysql.h"
#include "./yxJSON.h"
#include "LinkedList.h"

#include <pthread.h>

#define MYSQL_RESULT_SUCCESS 0
#define MYSQL_RESULT_ERROR   1

typedef MYSQL Connection;

typedef struct {
    pthread_mutex_t lock;
    pthread_cond_t  cond;
    LinkedList     *connections;
    char           *host;
    int             port;
    char           *user;
    char           *password;
    char           *database;
} DataSource;

typedef struct {
    int         type;
    yxObject   *value;
    const char *errMsg;
} MYSQLResult;

DataSource  *createDataSource(char *host, int port, char *user, char *password, char *database);
int          addConnection(DataSource *ds);
Connection  *getConnection(DataSource *ds);
void         returnConnection(DataSource *ds, Connection *connection);
void         closeConnection(Connection *connection);
void        *exceuteQuery(Connection *connection, char *sql);
MYSQLResult *createMYSQLErrResult(const char *err);
MYSQLResult *createMYSQLSuccessResult(void *result);
const char  *yxMYSQLResultGetErrMsg(MYSQLResult *result);
char        *yxMYSQLResultGetStringValue(MYSQLResult *result);
double       yxMYSQLResultGetDoubleValue(MYSQLResult *result);
int          yxMYSQLResultGetIntValue(MYSQLResult *result);
yxObject    *yxMYSQLResultGetObjectValue(MYSQLResult *result);
yxArray     *yxMYSQLResultGetArrayValue(MYSQLResult *result);
void         yxMYSQLResultFree(MYSQLResult *result);
void         yxMYSQLResultFreeNotValue(MYSQLResult *result);

#endif // YXMYSQL_H
